package com.cpi.cornpp.grinddistributionversion.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.grinddistributionversion.exception.GrindDistributionVersionException;
import com.cpi.cornpp.grinddistributionversion.form.GrindDistributionVersionForm;
import com.cpi.cornpp.grinddistributionversion.services.GrindDistributionVersionService;
import com.cs.common.utils.NumberUtil;

/**
 * Grind Priority Distribution Version Action
 * @author Edgar Joao
 *
 */
public class GrindDistributionVersionAction extends MappingDispatchAction{
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Id de version */
	private static final String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Custom exception */
	private static final String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_UPDATE_LABEL = "common.edit.success";//$NON-NLS-1$
	private static final String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private final static String PERIOD_SELECTED_ID = "periodId";//$NON-NLS-1$
	public ActionForward showList(ActionMapping argMapping, ActionForm argForm, HttpServletRequest argRequest, HttpServletResponse argResponse) throws GrindDistributionVersionException {
		HttpSession httpSession = argRequest.getSession();
		GrindDistributionVersionService service = null;
		GrindDistributionVersionForm grindForm = null;
		int versionId = 0;
		List grindDistributionVersionList = null;
		String periodId = null;
		try{
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			grindDistributionVersionList = new ArrayList(0);
			service = new GrindDistributionVersionService();
		    grindForm = (GrindDistributionVersionForm) argForm;
		    periodId = argRequest.getParameter(PERIOD_SELECTED_ID);
			if( periodId == null ){
				grindDistributionVersionList = service.getGrindDistributionVersion(versionId, 0);
			}else{
				grindDistributionVersionList = service.getGrindDistributionVersion(versionId, Integer.parseInt(periodId));
			}
			grindForm.setGrindDistributionVersion(grindDistributionVersionList);
			grindForm.reset(argMapping, argRequest);
		}catch (GrindDistributionVersionException grindDistributionVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, grindDistributionVersionException);
			throw grindDistributionVersionException;
		}catch (Exception exception){
			GrindDistributionVersionException grindDistributionVersionException = null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, grindDistributionVersionException);
			throw grindDistributionVersionException;
		}
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward save(ActionMapping argMapping, ActionForm argForm, HttpServletRequest argRequest, HttpServletResponse argResponse) throws GrindDistributionVersionException {
		HttpSession httpSession = null;
		GrindDistributionVersionService service = null;
		GrindDistributionVersionForm grindForm = null;
		String messageError = null;
		MessageResources messageResources = null;
		try{
			httpSession = argRequest.getSession();
			messageResources = getResources(argRequest);
			grindForm = (GrindDistributionVersionForm)argForm;
			service = new GrindDistributionVersionService();
			service.save(grindForm.getGrindDistributionVersion());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_UPDATE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (GrindDistributionVersionException grindDistributionVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, grindDistributionVersionException);
			throw grindDistributionVersionException;
		}catch (Exception exception){
			GrindDistributionVersionException grindDistributionVersionException = null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, grindDistributionVersionException);
			throw grindDistributionVersionException;
		}
		return argMapping.findForward(DEFAULT_FORWARD);
	}
}
